package com.jlkf.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.pojo.TblOtherCourse;
import com.jlkf.pojo.TblStudentStory;
import com.jlkf.service.StudentStoryService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Controller
@RequestMapping("studentStory")
public class StudentStoryController {
	@Autowired
	private StudentStoryService studentStoryService;
	
	/**
	 * 学员故事列表
	 * @param startTime
	 * @param endTime
	 * @param name
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getList")
	@ResponseBody
	public ReturnData getList(String startTime,String endTime,String name,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		if(!StringTools.isEmpty(startTime)){
			map.put("startTime", startTime);
		}
		if(!StringTools.isEmpty(endTime)){
			map.put("endTime", endTime);
		}
		if(!StringTools.isEmpty(name)){
			map.put("name", name);
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblStudentStory> list = studentStoryService.getList(map);
		PageInfo<TblStudentStory> pageInfo = new PageInfo<>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"学员故事列表");
	}
	
	/**
	 * 查看详情
	 */
	@RequestMapping("getDetail")
	@ResponseBody
	public ModelAndView getDetail(Integer gId){
		TblStudentStory story = studentStoryService.getDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("story",story);
    	andView.setViewName("jsp/studentStoryManager/addOrUpdate");
		return andView;
	}
	
	/**
	 * 新增或编辑
	 * @param json
	 * @return
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public ReturnData addOrUpdate(@RequestBody String json){
		try{
			TblStudentStory story = StringTools.json2Obj(json, TblStudentStory.class);
			if(story.getgId()!=null){
				int ret = studentStoryService.update(story);
				if(ret>0){
					return new ReturnData(200,"修改成功");
				}else{
					return new ReturnData(500,"修改失败");
				}
			}else{
				story.setgCreateTime(new Date());
				int ret = studentStoryService.insert(story);
				if(ret>0){
					return new ReturnData(200,"添加成功");
				}else{
					return new ReturnData(500,"添加失败");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"参数传入有误");
		}
	}
	
	/**
	 * 删除
	 * @param gId
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public ReturnData delete(Integer gId){
		int rel = studentStoryService.delete(gId);
		if(rel>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
}
